iT邦幫忙

2021 iThome 鐵人賽

DAY 30
0
自我挑戰組

IOS、Python自學心得30天系列 第 30

IOS、Python自學心得30天 Day-30 簡單辨識範例

  • 分享至 

  • xImage
  •  

範例如下:

程式碼:

//
//  ViewController.swift
//  Dog Breed Recognition App
//
//  Created by PecaJo on 2021/6/24.

import UIKit
import FirebaseStorage
import FirebaseDatabase

class ViewController: UIViewController {
    
    @IBOutlet weak var uploadButton: UIButton!
    @IBOutlet weak var libraryButton: UIButton!
    @IBOutlet weak var imageview: UIImageView!
    @IBOutlet weak var dogNameLabel: UILabel!
    @IBOutlet weak var dogAccLabel: UILabel!
    @IBOutlet weak var loadingLabel: UILabel!
    
    let imagePicker = UIImagePickerController()
    
    var ref: DatabaseReference! = Database.database().reference()
    var tmpBool = false
    var detect = "False"
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.imagePicker.delegate = self
        DispatchQueue.global().async {
            while(true){
                sleep(1)
                if self.tmpBool {
                    self.uploadasync()
                }
            }
        }
    }
    
    @IBAction func clickuploadbutton(_ sender: Any) {
        if imageview.image != nil {
            self.loadingLabel.isHidden = false
            uploadFile()
            ref.child("DetectBool/Bool/").setValue("True")
            ref.child("dogName/Name/").setValue("")
            ref.child("dogAcc/Acc/").setValue("")
            tmpBool = true
        }
        else{
            let alertController = UIAlertController(title: "圖片訊息", message: "請放入圖片", preferredStyle: UIAlertController.Style.alert)
            alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
            present(alertController, animated: true, completion: nil)
        }
    }
    @IBAction func clicklibrarybutton(_ sender: Any) {
        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true)
    }
    
    func uploadFile(){
        let url = URL(string: "https://XXXXXXXXXXXXXXXX")
        // Create a root reference
        let storageRef = Storage.storage().reference()

        // Data in memory
        let imageData: Data = imageview.image!.jpegData(compressionQuality: 1.0)!

        // Create a reference to the file you want to upload
        let riversRef = storageRef.child("dog.jpg")

        // Upload the file to the path "images/rivers.jpg"
        let uploadTask = riversRef.putData(imageData, metadata: nil) { (metadata, error) in
          guard let metadata = metadata else {
            // Uh-oh, an error occurred!
            return
          }
          // Metadata contains file metadata such as size, content-type.
          let size = metadata.size
          // You can also access to download URL after upload.
          riversRef.downloadURL { (url, error) in
            guard let downloadURL = url else {
              // Uh-oh, an error occurred!
              return
            }
          }
        }
    }
    func uploadasync(){
        DispatchQueue.main.async { [self] in
            self.ref.child("DetectBool/Bool/").observeSingleEvent(of: .value) {
                (snapshot) in
                self.detect = (snapshot.value as? String)!
                print(self.detect)
            }
            if self.dogNameLabel.text != "" {
                if self.dogAccLabel.text != "" {
                    if self.detect == "False" {
                        self.loadingLabel.isHidden = true
                    }
                }
            }
            self.ref.child("dogName/Name/").observeSingleEvent(of: .value) {
                (snapshot) in
                if let name = snapshot.value as? String {
                    self.dogNameLabel.text = "\(name)"
                }
            }
            self.ref.child("dogAcc/Acc/").observeSingleEvent(of: .value) {
                (snapshot) in
                if let acc = snapshot.value as? String {
                    self.dogAccLabel.text = "\(acc)"
                }
            }
            
        }
    }
}

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
            imageview.contentMode = .scaleAspectFit
            imageview.image = pickedImage
            imageview.layer.masksToBounds = true;
        }
        dismiss(animated: true, completion: nil)
    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
}

其餘功能會在之後繼續增加
對於自己30天的紀錄與挑戰
到這告一段落
未來還會繼續增進自己的能力
謝謝觀看


上一篇
IOS、Python自學心得30天 Day-29 連接Firebase辨識
系列文
IOS、Python自學心得30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言